package com.baizhi.controller;


import com.baizhi.entity.User;
import com.baizhi.service.IUserService;
import com.baizhi.util.JwtUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhenggang
 * @since 2021-09-30
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService us;
    @GetMapping("/login/{name}/{pwd}")
    //登陆方法，返回值要有token，还要返回用户名
    public Map<String,String> login(@PathVariable("name") String name,@PathVariable("pwd") String pwd){
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("username",name).eq("password",pwd);
        User user = us.getOne(wrapper);
        Map<String,String> resultMap=new HashMap<>();
        if(user!=null){
            //token里面要存储的数据
            Map<String,String> map = new HashMap();
            map.put("username",user.getUsername());
            //生成token
            String token = JwtUtils.getToken(map);
            resultMap.put("token",token);
            resultMap.put("username",user.getUsername());
            resultMap.put("msg","登陆成功");
        }else{
            resultMap.put("msg","登录失败");
        }

        return resultMap;
    }
}

